function flatten(array){
    const res = []
    let stack = [...array]
    while(stack.length){
        const item = stack.pop()
        if(Array.isArray(item)){
            stack.push(...item)
        }else{
            res.push(item)
        }
    }
    return res.reverse()
}

function flatten1(array){
    const res = []
    for (const num of array) {
        if(Array.isArray(num)){
            res.push(...flatten1(num))
        }else {
            res.push(num)
        }
    }
    return res
}


const flattenArray = flatten1([1,[2,3,[4,5],5],6])
console.log(flattenArray)
